package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/grtqmxcontroller")
public class grtqmxcontroller {

	private static final Logger logger = Logger.getLogger(grtqmxcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response , String limit,HttpSession session,String id,String start,String end,String SJ) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
    
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		String sj1[]=SJ.split(" - ");
		if(sj1[0]!=null)start=sj1[0].toString().replaceAll("-","");
		if(sj1[1]!=null)end=sj1[1].toString().replaceAll("-", "");
		start+="01";
		end+="31";
		Map reqMap = new HashMap();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ121");
		//接口参数
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setGRZH(sessionIn.getGRZH());
		b.setQSRQ(start);
		b.setZZRQ(end);
		reqMap.put("BODY",b);
		String json = JSONUtil.ToJsongString2(reqMap);
		//请求
		String  result = HttpRequest.doPost(interfaceUrl, json);
		Map retmap=JSONUtil.toMap(result);
		logger.info("接口返回内容:"+retmap);
		if(retmap.get("RETURNCODE").equals("100")){
		Map resMap=(Map) retmap.get("body");
		List retlist=(List) resMap.get("RETDATA");
		for(int i=0;i<retlist.size();i++){
			Map map= (Map) retlist.get(i);
			map.put("XINGMING",resMap.get("XINGMING").toString());
		}
		logger.info("接口返回内容:"+result);
		if(result != null){
			if(retmap.get("RETURNCODE").equals("100")){
				//map.put("count",retlist.size());
				map.put("code", 0);
				map.put("msg", null);
				map.put("data", retlist);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("CW", retmap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
			
		}
		
		}else{
			map.put("code", 1);
			map.put("CW", retmap.get("RETURNDESC").toString());
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String jsonString = JSONUtil.ToJsongString2(map);
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			return null;
		}
		return null;
	}
	@RequestMapping("/test2")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response , String limit ,String id,String start,String end,HttpSession session,Map retMap) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			Calendar a=Calendar.getInstance();
			if(sessionIn!=null){
				if(sessionIn.getXINGMING()!=null && sessionIn.getZJHM()!=null && sessionIn.getDWMC()!=null && sessionIn.getGRZH()!=null){
					retMap.put("XINGMING", sessionIn.getXINGMING());
					retMap.put("ZJHM", sessionIn.getZJHM());
					retMap.put("DWMC", sessionIn.getDWMC());
					retMap.put("GJJZH", sessionIn.getGRZH());
					map.put("code", 0);
					map.put("msg", null);
					map.put("data", retMap);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}else{
					map.put("code", 1);
					map.put("CW", "登录超时请重新登录");
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
				}
			}else{
				map.put("code", 1);
				map.put("CW", "登录超时请重新登录");
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
			
			return null;
	}
}
